#!/bin/bash
if [ $# != 4 ]
then
echo "please input path"
echo "Usage: bash ImageNet2012Preprocess/ImageNet2012Preprocess.sh  [/path/ILSVRC2012_img_train.tar] [/path/ILSVRC2012_img_val.tar] [/path/train] [/path/val] " 
exit 1
fi

train_data_tar_path=$1
val_data_tar_path=$2
train_save_path=$3
val_save_path=$4
current_path=$(pwd)

if [ ${train_data_tar_path##*/} = "ILSVRC2012_img_train.tar" ]
then
	echo "ILSVRC2012_img_train.tar path is ok"
else
	echo "ILSVRC2012_img_train.tar path is not right ,please input right path"
	echo "such as /path/ILSVRC2012_img_train.tar "
	exit 1
fi

if [ ${val_data_tar_path##*/} = "ILSVRC2012_img_val.tar" ]
then
	echo "ILSVRC2012_img_val.tar path is ok"
else
	echo "ILSVRC2012_img_val.tar path is not right ,please input right path"
	echo "such as /path/ILSVRC2012_img_val.tar"
	exit 1
fi


mkdir -p $train_save_path  $val_save_path 
tar -xvf $train_data_tar_path -C $train_save_path
tar -xvf $val_data_tar_path -C $val_save_path
cd $train_save_path
find . -name "*.tar" | while read LINE ; do mkdir -p "${LINE%.tar}"; tar -xvf "${LINE}" -C "${LINE%.tar}"; rm -f "${LINE}"; done
cd $current_path

python3 ImageNet2012PreprocessV1/preprocess_imagenet_validation_data.py $val_save_path ImageNet2012PreprocessV1/imagenet_2012_validation_synset_labels.txt
